package com.gamecollection.fish.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Objects;

/**
 * 游戏工具类
 */
public class GameUtils {
    
    // 方向控制
    public static boolean UP = false;
    public static boolean DOWN = false;
    public static boolean LEFT = false;
    public static boolean RIGHT = false;
    
    // 分数
    public static int count = 0;
    
    // 关卡等级
    public static int level = 0;
    
    // 背景图
    public static Image bgimg;
    
    // 敌方鱼类图片
    public static Image enamy1_img;
    public static Image enamyr_img;
    public static Image enamyl_2img;
    public static Image enamyr_2img;
    public static Image enamyl_3img;
    public static Image enamyr_3img;
    public static Image bossimg;
    
    // 我方鱼类图片
    public static Image MyFishimg_L;
    public static Image MyFishimg_R;
    
    // 静态初始化块
    static {
        try {
            bgimg = loadImage("/img/fish/sea.jpg");
            enamy1_img = loadImage("/img/fish/enemyFish/fish1_r.gif");
            enamyr_img = loadImage("/img/fish/enemyFish/fish1_l.gif");
            enamyl_2img = loadImage("/img/fish/enemyFish/fish2_r.png");
            enamyr_2img = loadImage("/img/fish/enemyFish/fish2_l.png");
            enamyl_3img = loadImage("/img/fish/enemyFish/fish3_r.png");
            enamyr_3img = loadImage("/img/fish/enemyFish/fish3_l.png");
            bossimg = loadImage("/img/fish/enemyFish/boss.gif");
            MyFishimg_L = loadImage("/img/fish/myFish/myfish_left.gif");
            MyFishimg_R = loadImage("/img/fish/myFish/myfish_right.gif");
        } catch (Exception e) {
            System.err.println("Failed to load fish images: " + e.getMessage());
            // 创建默认图片
            bgimg = createDefaultImage(Color.BLUE, 1440, 900);
            enamy1_img = createDefaultImage(Color.RED, 50, 30);
            enamyr_img = createDefaultImage(Color.RED, 50, 30);
            enamyl_2img = createDefaultImage(Color.ORANGE, 60, 40);
            enamyr_2img = createDefaultImage(Color.ORANGE, 60, 40);
            enamyl_3img = createDefaultImage(Color.YELLOW, 70, 50);
            enamyr_3img = createDefaultImage(Color.YELLOW, 70, 50);
            bossimg = createDefaultImage(Color.MAGENTA, 100, 80);
            MyFishimg_L = createDefaultImage(Color.GREEN, 60, 40);
            MyFishimg_R = createDefaultImage(Color.GREEN, 60, 40);
        }
    }
    
    /**
     * 绘制文字的工具类
     */
    public static void drawWord(Graphics g, String str, Color color, int size, int x, int y) {
        g.setColor(color);
        g.setFont(new Font("仿宋", Font.BOLD, size));
        g.drawString(str, x, y);
    }
    
    /**
     * 加载图片
     */
    public static Image loadImage(String path) {
        try {
            Image img = ImageIO.read(Objects.requireNonNull(GameUtils.class.getResource(path)));
            if (img == null) {
                System.out.println("图片加载失败: " + path);
                return createDefaultImage(Color.GRAY, 50, 50);
            }
            return img;
        } catch (Exception e) {
            System.out.println("图片加载失败: " + path);
            return createDefaultImage(Color.GRAY, 50, 50);
        }
    }
    
    /**
     * 创建默认图片
     */
    private static Image createDefaultImage(Color color, int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(color);
        g2d.fillRect(0, 0, width, height);
        g2d.dispose();
        return img;
    }
} 